package com.yydream.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yydream.common.constant.HttpStatus;
import com.yydream.common.utils.PageUtils;
import com.yydream.common.utils.R;
import com.yydream.dto.Paging;
import com.yydream.dto.SysCategoryDTO;
import com.yydream.dto.SysCategoryPageDTO;
import com.yydream.entity.SysCategoryEntity;
import com.yydream.mapper.SysCategoryMapper;
import com.yydream.service.SysCategoryService;
import com.yydream.vo.SysCategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SysCategoryServiceImpl extends ServiceImpl<SysCategoryMapper, SysCategoryEntity> implements SysCategoryService {
    @Override
    public R queryById(Long id) {
        SysCategoryEntity sysCategoryEntity = this.getById(id);
        if (Objects.isNull(sysCategoryEntity)) {
            return R.error(HttpStatus.DATA_NOT_EXIST);
        }
        SysCategoryVo sysCategoryVo = new SysCategoryVo();
        BeanUtils.copyProperties(sysCategoryEntity, sysCategoryVo);
        return R.ok().put( sysCategoryVo);
    }

    @Override
    public R pageList(SysCategoryPageDTO req) {
        Page<SysCategoryEntity> page = PageUtils.page(req);
        LambdaQueryWrapper<SysCategoryEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(req.getName()), SysCategoryEntity::getName, req.getName())
                .like(!StringUtils.isEmpty(req.getRemark()),SysCategoryEntity::getRemark, req.getRemark());
        Page<SysCategoryEntity> list = this.page(page, wrapper);
        Paging<SysCategoryVo> sysCategoryVoPaging = PageUtils.pageCopy(list, SysCategoryVo.class);
        return R.ok().put( sysCategoryVoPaging);
    }

    @Override
    public R bashInsertByNames(List<String> names) {
        List<Long> listIds = new ArrayList<>();
        names.forEach(c -> {
            SysCategoryEntity sysCategoryEntity = new SysCategoryEntity();
            sysCategoryEntity.setName(c);
            SysCategoryEntity categoryEntity = this.getOne(new LambdaQueryWrapper<SysCategoryEntity>().eq(SysCategoryEntity::getName, c));
            if (Objects.isNull(categoryEntity)) {
                this.baseMapper.insert(sysCategoryEntity);
                listIds.add(sysCategoryEntity.getId());
            } else {
                listIds.add(categoryEntity.getId());
            }
        });
        return R.ok().put( listIds);
    }

    @Override
    public R insert(SysCategoryDTO req) {
        SysCategoryEntity sysCategoryEntity = new SysCategoryEntity();
        BeanUtils.copyProperties(req, sysCategoryEntity);
        LambdaQueryWrapper<SysCategoryEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysCategoryEntity::getName, req.getName());
        List<SysCategoryEntity> list = this.list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return R.error(HttpStatus.ALREADY_EXIST);
        }
        boolean save = this.save(sysCategoryEntity);
        return R.ok().put( save);
    }

    @Override
    public R update(SysCategoryDTO req) {
        SysCategoryEntity entity = this.getById(req.getId());
        if (Objects.isNull(entity)) {
            return R.error(HttpStatus.DATA_NOT_EXIST);
        }
        BeanUtils.copyProperties(req, entity);
        LambdaQueryWrapper<SysCategoryEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysCategoryEntity::getName, req.getName())
                .ne(SysCategoryEntity::getId, entity.getId());
        List<SysCategoryEntity> list = this.list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return R.error(HttpStatus.ALREADY_EXIST);
        }
        boolean update = this.updateById(entity);
        return R.ok().put( update);
    }

    @Override
    public R deleteById(Long[] ids) {
        List<SysCategoryEntity> collect = Arrays.stream(ids).map(c -> {
            SysCategoryEntity sysCategoryEntity = new SysCategoryEntity();
            sysCategoryEntity.setId(c);
            return sysCategoryEntity;
        }).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(collect)) {
            return R.error(HttpStatus.PARAM_ERROR);
        }
        boolean b = this.removeBatchByIds(collect);
        return R.ok().put( b);
    }

}
